﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

public class wxMiniProgramLogin : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        //       string authHeader = context.Request.Headers["token"];//Header中的token
        //       if (string.IsNullOrEmpty(authHeader)||jo==null)
        //       {
        //            HttpContext.Current.Response.Write("parameter error");
        //          return;
        //       }


        var reserveId=HttpContext.Current.Request.Form["reserveId"];
        var memberId=HttpContext.Current.Request.Form["memberId"];
        var Action=HttpContext.Current.Request.Form["Action"];

        using (Entities db = new Entities())
        {

      
            var status="http://wq.db115.com/api/status_jiuda.php?dev_id="+reserveId;

            var kt="http://wq.db115.com/api/opendoor_jiuda.php?dev_id="+reserveId;
            var guanb="http://wq.db115.com/api/closedoor_jiuda.php?dev_id="+reserveId;

            var code = 1;
            var msg = "";
            var time = DateTime.Now;

            var thestatus="";
            var result="";
            if(Action=="close")
            {
                thestatus=webPost(status);
                if (thestatus != "")
                {
                    webPost(status);
                    result = webPost(guanb);
                    msg = "关锁成功";
                }
                else
                { 
                    msg = "电子锁不在线，请联系服务员";  
                    
                    }
            }
            else
            {

                thestatus=webPost(status);
                if(thestatus!="")
                {
                    result=webPost(kt);
                    msg = "开锁成功";
                }
                  else
                { 
                    msg = "电子锁不在线，请联系服务员";  
                    
                    }
            }
            context.Response.Write(JsonConvert.SerializeObject(new { code,msg,time,result}));

            HttpContext.Current.Response.End();



            //   sendSucceMsg(_rky.getOpenIDByguid(UparentId), parOrderID.ToString(), uguid);
            //  sendSucceMsgTome(_rky.getOpenIDByguid(uguid), parOrderID.ToString(), UparentId);



        }

    }



    private static string webPost(string url, string obj = "")
    {
        string param = (obj);//参数
        byte[] bs = Encoding.Default.GetBytes(param);
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "application/json";
        req.ContentLength = bs.Length;
        using (Stream reqStream = req.GetRequestStream())
        {
            reqStream.Write(bs, 0, bs.Length);
            reqStream.Close();
            HttpWebResponse response2 = (HttpWebResponse)req.GetResponse();
            StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.UTF8);
            string result = sr2.ReadToEnd();
            return result;
        }
    }


    public bool IsReusable {
        get {
            return false;
        }
    }

}